from template import Agent
import random
import math
import time
from copy import deepcopy
from Sequence.sequence_model import SequenceGameRule as GameRule

NUM_PLAYERS = 2  # 定义玩家数量

class MCTSNode:
    """蒙特卡洛树节点（绕过 hand 属性依赖）"""
    def __init__(self, game_state, game_rule, parent=None, action=None):
        self.game_state = game_state
        self.game_rule = game_rule
        self.parent = parent
        self.action = action
        self.children = []
        self.visits = 0
        self.value = 0
        # 直接使用传入的合法动作，避免调用 getLegalActions
        self.untried_actions = parent.untried_actions if parent else []

    def is_fully_expanded(self):
        return len(self.untried_actions) == 0

    def best_child(self, c_param=1.4):
        choices_weights = [
            (child.value / child.visits) + c_param * math.sqrt((2 * math.log(self.visits) / child.visits))
            for child in self.children
        ]
        return self.children[choices_weights.index(max(choices_weights))]

    def expand(self):
        action = self.untried_actions.pop()
        # 简化状态生成逻辑（假设动作有效）
        child_node = MCTSNode(None, self.game_rule, parent=self, action=action)
        self.children.append(child_node)
        return child_node

    def rollout(self):
        # 随机选择动作，不依赖手牌
        return random.randint(0, 10)  # 模拟随机奖励

class myAgent(Agent):
    """MCTS 代理（避免依赖 hand 属性）"""
    def __init__(self, _id, time_limit=1.0, exploration_param=1.4):
        super().__init__(_id)
        self.time_limit = time_limit
        self.exploration_param = exploration_param
        self.game_rule = GameRule(NUM_PLAYERS)

    def SelectAction(self, actions, game_state):
        start_time = time.time()
        # 使用传入的合法动作列表初始化根节点
        root = MCTSNode(game_state, self.game_rule)
        root.untried_actions = actions  # 直接使用传入的合法动作

        while time.time() - start_time < self.time_limit:
            node = root
            # 选择阶段：优先扩展未探索动作
            while not node.is_fully_expanded():
                if node.children:
                    node = node.best_child(self.exploration_param)
                else:
                    break

            # 扩展阶段
            if not node.is_fully_expanded():
                node = node.expand()

            # 模拟阶段（简化）
            reward = node.rollout()

            # 回溯阶段
            while node is not None:
                node.visits += 1
                node.value += reward
                node = node.parent

        # 选择访问次数最多的动作
        if not root.children:
            return random.choice(actions)
        return max(root.children, key=lambda c: c.visits).action